package com.example.lib_common.mvi.custom

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import android.view.animation.RotateAnimation
import android.widget.ImageView
import android.widget.RelativeLayout
import com.example.lib_common.R

class LoadDialog(context: Context) : Dialog(context) {
    private var rotateAnimation: RotateAnimation
    private lateinit var rootView: RelativeLayout
    private lateinit var contentView: RelativeLayout
    private lateinit var img: ImageView
    init {
        rotateAnimation =
            AnimationUtils.loadAnimation(getContext(), R.anim.load_anim) as RotateAnimation
        rotateAnimation.reset()
        rootView = RelativeLayout(getContext())
        val lp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT)
        rootView.layoutParams = lp
        contentView = RelativeLayout(getContext())
        val contentLp: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(
            200,100)
        contentLp.addRule(RelativeLayout.CENTER_VERTICAL)
        contentView.layoutParams = contentLp
        rootView.addView(contentView)
        img = ImageView(getContext())

        val imgLp: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(
            80,80)
        imgLp.addRule(RelativeLayout.CENTER_IN_PARENT)
        img.layoutParams = imgLp
        contentView.addView(img)
        img.setImageResource(R.drawable.res_icon_240)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(rootView)
    }

    override fun show() {
        super.show()
        img.startAnimation(rotateAnimation)
    }

    override fun dismiss() {
        super.dismiss()
        rotateAnimation.cancel()
        img.clearAnimation()
    }
}